﻿Public Class FrmSaat

    Dim Personel As Integer

    Sub New(ByVal Personel As Integer)
        InitializeComponent()
        ToolBar.Renderer = New FirefoxRenderer
        BtnHelp.DropDown = HelpMenu
        BtnAdd.Image = BtnAdd.Image.Resize(16, 16)
        List.AddColumn("tarih", "Tarih", "30%", "c")
        List.AddColumn("saat1", "Başlangıç", "20%", "c")
        List.AddColumn("saat2", "Bitiş", "20%", "c")
        List.AddColumn("sure", "Süre", "20%", "c")
        List.AddColumn("toplam", "Toplam", "20%", "c")
        List.CellBorderStyle = DataGridViewCellBorderStyle.Single
        BtnAdd.Enabled = Kullanici.Operator
        BtnDelete.Enabled = Kullanici.Operator
        Icon = Icon.FromHandle(My.Resources.saat.GetHicon)
        Me.Personel = Personel
        Reload()
    End Sub

    Sub Reload()
        List.Rows.Clear()
        Dim Toplam As Decimal = 0
        For Each Item In Service.Izin.SaatlikListe(Personel)
            List.Rows.Add()
            Dim Row As DataGridViewRow = List.Rows(List.Rows.Count - 1)
            If Item.Saat1 = "" Then
                Row.Cells("tarih").Value = "Bakiye"
                Toplam = Item.SaatBakiyesi
            Else
                Row.Tag = Item.Kod
                Row.Cells("tarih").Value = Item.Tarih.DMY
                Row.Cells("saat1").Value = Mid(Item.Saat1, 1, 2) & ":" & Mid(Item.Saat1, 3, 2)
                Row.Cells("saat2").Value = Mid(Item.Saat2, 1, 2) & ":" & Mid(Item.Saat2, 3, 2)
                Dim Fark = SaatHesapla(Item.Saat1, Item.Saat2)
                Row.Cells("sure").Value = FormatSaat(Fark)
                Toplam += Fark
            End If
            Row.Cells("toplam").Value = FormatSaat(Toplam)
        Next
    End Sub

    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Close()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Dim Form As New FrmIzin("S")
        Form.SetPersonel(Personel)
        Form.ShowDialog()
        Reload()
    End Sub

    Private Sub BtnIzinDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If List.SelectedRows.Count = 0 Then Exit Sub
        If Val(List.SelectedRows(0).Tag) = 0 Then Warning("Bu kaydı silemezsiniz.") : Exit Sub
        If Not Confirm("Seçilmiş olan izin kaydının silinmesini istediğinizden emin misiniz?") Then Exit Sub
        Service.Izin.Sil(List.SelectedRows(0).Tag, Kullanici.Kod)
        Reload()
    End Sub

    Private Sub ToolBar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ToolBar.Paint
        FirefoxRenderer.PaintBackground(e.Graphics, sender.ClientRectangle)
    End Sub

End Class